/*
 * Copyright (c) 2017年11月09日  by XuanWu Wireless Technology Co., Ltd 
 *             All rights reserved  
 */
package com.zml.ninth.example2;

import java.awt.*;
import java.util.Iterator;
import java.util.Stack;

/**
 * @author <a href="mailto:zhaominglin@wxchina.com">minglin.Zhao</a>
 * @Description
 * @Date 2017/11/9
 * @Version 1.0.0
 */
public class CompositeIterator implements Iterator{
    Stack stack = new Stack();

    public CompositeIterator(Iterator iterator){
        stack.push(iterator);
    }

    @Override
    public boolean hasNext() {
        if(stack.empty()){
            return false;
        }else {
            Iterator iterator = (Iterator)stack.peek();
            if(!iterator.hasNext()){
               stack.pop();
               return hasNext();
            }else {
                return true;
            }
        }
    }

    @Override
    public Object next() {
        if(hasNext()){
            Iterator iterator = (Iterator)stack.peek();
            MenuComponent menuComponent = (MenuComponent)iterator.next();
            if(menuComponent instanceof Menu){
//                stack.push(menuComponent.createIterator());
            }
            return menuComponent;
        }
        return null;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}
